package com.cb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.common.R;
import com.cb.entity.Category;
import com.cb.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //新增分类
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category:{}",category);
        categoryService.save(category);
        return R.success("新增分类成功！");
    }

    /**
     * 分类信息分页查询
     */
    @GetMapping("/page")
    public R<IPage<Category>> page(int page, int pageSize){
        log.info("page={},pageSize={}", page, pageSize);

        //构造分页构造器
        Page<Category> categoryPage = new Page<>(page, pageSize);
        IPage<Category> page1=categoryService.getCategory(categoryPage);
        return R.success(page1);
    }

    /**
     * 根据id删除分类
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除分类，id为{}",ids);
//        categoryService.removeById(ids);
        categoryService.remove(ids);
        return R.success("分类信息删除成功！");
    }

    /**
     * 修改分类
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("分类修改成功！");
    }

    /**
     * 根据条件查询分类数据
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        List<Category> list=categoryService.getList(category);
        return R.success(list);
    }

}